//
//  WBHomeViewController.swift
//  新浪微博
//
//  Created by 名品导购网MPLife.com on 2017/11/20.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
//定义全局常量 尽量使用private修饰 否则到处都可以访问
 private let originalCellId = "originalCellId"
 private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(browserPhoto), name: NSNotification.Name(rawValue: WBStatusCellBrowserPhotoNotification), object: nil)
        
    }
    deinit {
        //注销通知
    NotificationCenter.default.removeObserver(self)
    }
//浏览照片通知监听方法
    @objc private func browserPhoto(n:NSNotification){
    //从通知的userInfo提取参数
        guard let selectedIndex = n.userInfo?[WBStatusCellBrowserPhotoSelectedIndexKey] as? Int,
            let urls = n.userInfo? [WBStatusCellBrowserPhotoURLsKey] as? [String],
            let imageList = n.userInfo? [WBStatusCellBrowserPhotoImageViewsKey] as?[UIImageView]
            else {
            return
        }
        //展现照片浏览控制器
        let vc = HMPhotoBrowserController.photoBrowser(withSelectedIndex: selectedIndex, urls: urls, parentImageViews: imageList)
        present(vc, animated: true, completion: nil)
    }
    //列表视图mo'x
    private lazy var listViewModel = WBStatusListViewModel()
    
    //闭包里面需要加self
    override func loadData() {
        
        refreshControl?.beginRefreshing()
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            //用AFN加载微博工具
            self.listViewModel .loadStatus(pullUp: self .isPullup) { (isSuccess, shouldRefresh) in
                self .refreshControl?.endRefreshing()
                //恢复上拉刷新标记
                self.isPullup = false
                //刷新表格
                
                if shouldRefresh {
                    self.tableView?.reloadData()
                }
                
            }
        }
    }
    @objc private func testClick(){
        print("测试")
        let vc = WBDemoViewController()
        //push的动作是nav执行的
        navigationController?.pushViewController(vc, animated: true)
    }
}
//表格数据源方法 具体的数据源方法实现，不需要supper
extension WBHomeViewController{
    //重写父类方法
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel .statusList .count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //取出视图模型判断用哪个Cell
        let  vm = listViewModel .statusList [indexPath.row]
        if (vm.status.retweeted_status != nil) {
       let cell =  tableView.dequeueReusableCell(withIdentifier: retweetedCellId, for: indexPath) as! WBStatusRetweetedCell
            cell.viewModel = vm
            cell.delegate = self
            cell .selectionStyle = .none
            return cell
        }else{
        let cell = tableView.dequeueReusableCell(withIdentifier: originalCellId, for: indexPath) as! WBStatusCell
            cell.viewModel = vm
            cell.delegate = self
            cell .selectionStyle = .none
            return cell
        }
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let  vm = listViewModel .statusList [indexPath.row]
        var cellHeight:CGFloat = 0
        vm.status.retweeted_status != nil ? (cellHeight = vm.retweetedCellHeight)  : (cellHeight = vm.oriangalCellHeight)
        return cellHeight
    }
    
}
extension WBHomeViewController:WBStatusCellDelegate,WBRetweetedCellDelegate{
    func statusCellDidSelectedUrlString(cell: WBStatusCell, urlStr: String) {
        let vc = WBWebViewController()
        vc.urlString = urlStr
        navigationController?.pushViewController(vc, animated: true)
        
    }
    func RetweetedCellDidSelectedUrlString(cell: WBStatusRetweetedCell, urlStr: String) {
        let vc = WBWebViewController()
        vc.urlString = urlStr
        navigationController?.pushViewController(vc, animated: true)
        
    }
}
//设置界面
extension WBHomeViewController{
    override func setUpTableView() {
        super .setUpTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(testClick))
        tableView?.register(WBStatusCell.self, forCellReuseIdentifier: originalCellId)
        tableView?.register(WBStatusRetweetedCell.self, forCellReuseIdentifier: retweetedCellId)
        tableView?.separatorStyle = .none
        setUpNavTitle()
    }
    //设置导航看标题
    private func setUpNavTitle(){
        let title = WBNetworkManager .shared .userAccount .screen_name
        let button = WBTitleButton(title: title)
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        navItem .titleView  = button
    }
    
    @objc func clickTitleButton(btn:UIButton){
        btn .isSelected = !btn .isSelected
    }
}
